package com.shrct.businessprocess.project.impl;

import com.shrct.businessprocess.project.RCCommissionYearConfig;
import com.shrct.businessprocess.project.RCSaleCommissionPayment;
import com.shrct.businessprocess.project.RCSaleCommissionPayment4CalcSettlementYear;
import com.shrct.businessprocess.project.RCSaleCommissionYearPayment;
import com.shrct.businessprocess.project.RCSalesCommissionYearConfig;
import com.shrct.businessprocess.project.RCSalesCommissionYearSettlement;
import com.shrct.businessprocess.project.impl.RCSaleCommissionPayment4SettlementYearImpl;
import com.shrct.businessprocess.project.impl.RCSalesCommissionYearConfigImpl;
import com.shrct.businessprocess.project.proxy.RCSaleCommissionPayment4CalcSettlementYearProxy;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCSALECOMMISSIONPAYMENT4CALCSETTLEMENTYEAR_TYPE")
public class RCSaleCommissionPayment4CalcSettlementYearImpl extends RCSaleCommissionPayment4SettlementYearImpl implements RCSaleCommissionPayment4CalcSettlementYear {
  public RCSaleCommissionPayment4CalcSettlementYearImpl() {
    super();
  }
  
  public RCSaleCommissionPayment4CalcSettlementYearImpl(final RCSaleCommissionPayment payment) {
    super( payment);
    
  }
  
  @JoinColumn(name = "contract_id")
  @ManyToOne(targetEntity = RCSalesCommissionYearConfigImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCSalesCommissionYearConfig contract;
  
  public RCSalesCommissionYearConfig getContract() {
    return this.contract;
  }
  
  public RCSaleCommissionPayment4CalcSettlementYear setContract(final RCSalesCommissionYearConfig contract) {
    this.contract = contract;
    return this;			
    
  }
  
  public Integer getSettlementYear() {
    RCCommissionYearConfig _overallConfig = this.contract.getOverallConfig();
    Integer _year = _overallConfig.getYear();
    return _year;
  }
  
  public Double getTotalAmountTarget() {
    RCSalesCommissionYearSettlement _settlement = this.contract.getSettlement();
    Double _totalAmountTarget = _settlement.getTotalAmountTarget();
    return _totalAmountTarget;
  }
  
  public Double getTotalPaidAmount() {
    Double _totalAmountPaid = this.contract.getTotalAmountPaid();
    return _totalAmountPaid;
  }
  
  public Double getTotalSettlementAmount() {
    Double _totalAmount = this.contract.getTotalAmount();
    return _totalAmount;
  }
  
  public Double getTax4ThisYear() {
    Double _xblockexpression = null;
    {
      RCSaleCommissionPayment _payment = this.getPayment();
      RCSaleCommissionYearPayment _yearPayment = _payment.getYearPayment();
      final Integer paymentYear = _yearPayment.getYear();
      IGenericQuery<RCSaleCommissionPayment4CalcSettlementYear> query=getObjectFactory().createGenericQuery(RCSaleCommissionPayment4CalcSettlementYear.class,"select e from com.shrct.businessprocess.project.impl.RCSaleCommissionPayment4CalcSettlementYearImpl as e   where (((e.contract) = (:contract)) and ((e.contract.overallConfig.year) = (:paymentYear))) and  e.active = 1      ").setParameter("contract",contract).setParameter("paymentYear",paymentYear);
      Collection<RCSaleCommissionPayment4CalcSettlementYear> _list = query.list();
      final Function1<RCSaleCommissionPayment4CalcSettlementYear,Double> _function = new Function1<RCSaleCommissionPayment4CalcSettlementYear,Double>() {
          public Double apply(final RCSaleCommissionPayment4CalcSettlementYear it) {
            Double _tax = it.getTax();
            return _tax;
          }
        };
      Iterable<Double> _map = IterableExtensions.<RCSaleCommissionPayment4CalcSettlementYear, Double>map(_list, _function);
      final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
      Double _nullsafe = NumberExtensions.nullsafe(_reduce);
      _xblockexpression = (_nullsafe);
    }
    return _xblockexpression;
  }
  
  public Double getTotalTax() {
    IGenericQuery<RCSaleCommissionPayment4CalcSettlementYear> query=getObjectFactory().createGenericQuery(RCSaleCommissionPayment4CalcSettlementYear.class,"select e from com.shrct.businessprocess.project.impl.RCSaleCommissionPayment4CalcSettlementYearImpl as e   where ((e.contract) = (:contract)) and  e.active = 1      ").setParameter("contract",contract);
    Collection<RCSaleCommissionPayment4CalcSettlementYear> _list = query.list();
    final Function1<RCSaleCommissionPayment4CalcSettlementYear,Double> _function = new Function1<RCSaleCommissionPayment4CalcSettlementYear,Double>() {
        public Double apply(final RCSaleCommissionPayment4CalcSettlementYear it) {
          Double _tax = it.getTax();
          return _tax;
        }
      };
    Iterable<Double> _map = IterableExtensions.<RCSaleCommissionPayment4CalcSettlementYear, Double>map(_list, _function);
    final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public static RCSaleCommissionPayment4CalcSettlementYear create(final RCSaleCommissionPayment payment, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCSaleCommissionPayment4CalcSettlementYear rCSaleCommissionPayment4CalcSettlementYear = new com.shrct.businessprocess.project.impl.RCSaleCommissionPayment4CalcSettlementYearImpl(
    	payment
    );
    objectFactory.create(rCSaleCommissionPayment4CalcSettlementYear);
    return rCSaleCommissionPayment4CalcSettlementYear;			
    
  }
  
  public static RCSaleCommissionPayment4CalcSettlementYear createRCSaleCommissionPayment4CalcSettlementYearImpl(final RCSaleCommissionPayment payment, final IObjectFactory objectFactory, final Procedure1<RCSaleCommissionPayment4CalcSettlementYear> init) {
    com.shrct.businessprocess.project.RCSaleCommissionPayment4CalcSettlementYear rCSaleCommissionPayment4CalcSettlementYear = new com.shrct.businessprocess.project.impl.RCSaleCommissionPayment4CalcSettlementYearImpl(
    	payment
    );
    objectFactory.create(rCSaleCommissionPayment4CalcSettlementYear);
    init.apply(rCSaleCommissionPayment4CalcSettlementYear);
    
    return rCSaleCommissionPayment4CalcSettlementYear;			
    
  }
  
  public RCSaleCommissionPayment4CalcSettlementYearProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCSaleCommissionPayment4CalcSettlementYearProxy proxy = new com.shrct.businessprocess.project.proxy.RCSaleCommissionPayment4CalcSettlementYearProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
